﻿using System;
using System.Drawing;
using System.Windows.Forms;
using Sunny.UI;
using Work.API.Dtos;
using WinFormsApp1.DrugManagement;

namespace WinFormsApp1
{
    public partial class MainForm : UIForm
    {
        private LoginDTO? _currentUser;

        public MainForm()
        {
            InitializeComponent();
            InitializeNavMenu();
        }

        public MainForm(LoginDTO? user = null)
        {
            InitializeComponent();
            _currentUser = user;
            InitializeNavMenu();
            CenterWelcomeLabel();

            // 更新欢迎信息
            if (_currentUser != null)
            {
                lblWelcome.Text = $"欢迎您: {_currentUser.NickName ?? _currentUser.Username}，使用系统请从左侧菜单选择功能";
            }
        }
        
        /// <summary>
        /// 将欢迎标签居中显示
        /// </summary>
        private void CenterWelcomeLabel()
        {
            // 使用Resize事件确保窗口大小改变时也保持居中
            panelContainer.Resize += (s, e) =>
            {
                if (panelContainer.Width > 0 && panelContainer.Height > 0)
                {
                    lblWelcome.Location = new Point(
                        Math.Max(0, (panelContainer.Width - lblWelcome.Width) / 2),
                        Math.Max(0, (panelContainer.Height - lblWelcome.Height) / 2)
                    );
                }
            };
            // 初始居中（延迟执行，确保panelContainer已初始化）
            this.Load += (s, e) =>
            {
                if (panelContainer.Width > 0 && panelContainer.Height > 0)
                {
                    lblWelcome.Location = new Point(
                        Math.Max(0, (panelContainer.Width - lblWelcome.Width) / 2),
                        Math.Max(0, (panelContainer.Height - lblWelcome.Height) / 2)
                    );
                }
            };
        }

        /// <summary>
        /// 初始化导航菜单
        /// </summary>
        private void InitializeNavMenu()
        {
            int yPos = 15;
            int buttonHeight = 42;
            int spacing = 4;
            int groupSpacing = 15;
            
            // 门诊管理菜单组标题
            lblClinicTitle = new UILabel
            {
                Text = "门诊管理",
                Font = new Font("Microsoft YaHei UI", 13F, FontStyle.Bold),
                ForeColor = Color.FromArgb(80, 160, 255),
                Location = new Point(10, yPos),
                Size = new Size(230, 32),
                Style = UIStyle.Blue,
                TextAlign = ContentAlignment.MiddleLeft,
                BackColor = Color.Transparent // 确保背景透明
            };
            panelMenu.Controls.Add(lblClinicTitle);
            panelMenu.Controls.SetChildIndex(lblClinicTitle, panelMenu.Controls.Count - 1); // 确保标题显示在最上层
            yPos += 35;
            
            // 门诊管理菜单按钮
            btnMedicalCard = CreateMenuButton("医疗卡办理", yPos);
            btnMedicalCard.Click += mnuMedicalCard_Click;
            panelMenu.Controls.Add(btnMedicalCard);
            yPos += buttonHeight + spacing;
            
            btnAppointment = CreateMenuButton("病人挂号", yPos);
            btnAppointment.Click += mnuAppointment_Click;
            panelMenu.Controls.Add(btnAppointment);
            yPos += buttonHeight + spacing;
            
            btnDoctorWorkbench = CreateMenuButton("门诊医生", yPos);
            btnDoctorWorkbench.Click += mnuDoctorWorkbench_Click;
            panelMenu.Controls.Add(btnDoctorWorkbench);
            yPos += buttonHeight + spacing;
            
            btnPrescription = CreateMenuButton("开药", yPos);
            btnPrescription.Click += mnuPrescription_Click;
            panelMenu.Controls.Add(btnPrescription);
            yPos += buttonHeight + spacing;
            
            btnOutpatientSettlement = CreateMenuButton("门诊结账", yPos);
            btnOutpatientSettlement.Click += mnuOutpatientSettlement_Click;
            panelMenu.Controls.Add(btnOutpatientSettlement);
            yPos += buttonHeight + spacing + groupSpacing; // 确保有足够间距
            
            // 药品管理菜单组标题
            lblDrugTitle = new UILabel
            {
                Text = "药品管理",
                Font = new Font("Microsoft YaHei UI", 13F, FontStyle.Bold),
                ForeColor = Color.FromArgb(80, 160, 255),
                Location = new Point(10, yPos),
                Size = new Size(230, 32),
                Style = UIStyle.Blue,
                TextAlign = ContentAlignment.MiddleLeft,
                BackColor = Color.Transparent // 确保背景透明，不被覆盖
            };
            panelMenu.Controls.Add(lblDrugTitle);
            panelMenu.Controls.SetChildIndex(lblDrugTitle, panelMenu.Controls.Count - 1); // 确保标题显示在最上层
            yPos += 35;
            
            // 药品管理菜单按钮
            btnDrugView = CreateMenuButton("药品查看", yPos);
            btnDrugView.Click += mnuDrugView_Click;
            panelMenu.Controls.Add(btnDrugView);
            yPos += buttonHeight + spacing;
            
            btnDrugInbound = CreateMenuButton("药品入库", yPos);
            btnDrugInbound.Click += mnuDrugInbound_Click;
            panelMenu.Controls.Add(btnDrugInbound);
            yPos += buttonHeight + spacing;
            
            btnDrugOutbound = CreateMenuButton("药品出库", yPos);
            btnDrugOutbound.Click += mnuDrugOutbound_Click;
            panelMenu.Controls.Add(btnDrugOutbound);
            yPos += buttonHeight + spacing;
            
            btnInspectionList = CreateMenuButton("查看检药单", yPos);
            btnInspectionList.Click += mnuInspectionList_Click;
            panelMenu.Controls.Add(btnInspectionList);
            yPos += buttonHeight + spacing;
            
            btnDispensedDrugs = CreateMenuButton("查看已发药品", yPos);
            btnDispensedDrugs.Click += mnuDispensedDrugs_Click;
            panelMenu.Controls.Add(btnDispensedDrugs);
        }
        
        /// <summary>
        /// 创建菜单按钮
        /// </summary>
        private UIButton CreateMenuButton(string text, int y)
        {
            return new UIButton
            {
                Text = text,
                Font = new Font("Microsoft YaHei UI", 10.5F),
                Location = new Point(10, y),
                Size = new Size(230, 42),
                Style = UIStyle.Blue,
                TextAlign = ContentAlignment.MiddleLeft,
                Cursor = Cursors.Hand
            };
        }

        private void mnuMedicalCard_Click(object? sender, EventArgs e)
        {
            using var dlg = new Form1();
            dlg.ShowDialog(this);
        }

        private void mnuAppointment_Click(object? sender, EventArgs e)
        {
            using var dlg = new AppointmentForm();
            dlg.ShowDialog(this);
        }

        private void mnuDoctorWorkbench_Click(object? sender, EventArgs e)
        {
            using var dlg = new DoctorWorkbenchForm();
            dlg.ShowDialog(this);
        }

        private void mnuPrescription_Click(object? sender, EventArgs e)
        {
            // 开药功能需要在医生工作台中选择预约后使用
            UIMessageBox.ShowInfo("请在医生工作台中选择已完成病历的预约，然后进行开药操作。");
        }

        private void mnuOutpatientSettlement_Click(object? sender, EventArgs e)
        {
            using var dlg = new OutpatientSettlementForm();
            dlg.ShowDialog(this);
        }

        #region 药品管理模块菜单事件

        /// <summary>
        /// 药品查看菜单项点击事件
        /// </summary>
        private void mnuDrugView_Click(object? sender, EventArgs e)
        {
            using var dlg = new DrugManagement.DrugViewForm();
            dlg.ShowDialog(this);
        }

        /// <summary>
        /// 药品入库菜单项点击事件
        /// </summary>
        private void mnuDrugInbound_Click(object? sender, EventArgs e)
        {
            using var dlg = new DrugManagement.DrugInboundForm();
            dlg.ShowDialog(this);
        }

        /// <summary>
        /// 药品出库菜单项点击事件
        /// </summary>
        private void mnuDrugOutbound_Click(object? sender, EventArgs e)
        {
            using var dlg = new DrugManagement.DrugOutboundForm();
            dlg.ShowDialog(this);
        }

        /// <summary>
        /// 查看检药单菜单项点击事件
        /// </summary>
        private void mnuInspectionList_Click(object? sender, EventArgs e)
        {
            using var dlg = new DrugManagement.InspectionListForm();
            dlg.ShowDialog(this);
        }

        /// <summary>
        /// 查看已发药品菜单项点击事件
        /// </summary>
        private void mnuDispensedDrugs_Click(object? sender, EventArgs e)
        {
            using var dlg = new DrugManagement.DispensedDrugsForm();
            dlg.ShowDialog(this);
        }

        #endregion

        private void lblWelcome_Click(object sender, EventArgs e)
        {

        }

        // 保留容器以便后续切换为嵌入式时复用，目前弹窗方式不使用 panelContainer
    }
}


